package com.dlm.service.impl;

import com.dlm.dao.Order;
import com.dlm.dao.Technician;
import com.dlm.dao.WorkTime;
import com.dlm.service.IOrderService;
import com.dlm.util.SMSUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * Created by chongsong on 15/2/1.
 * <mailto>chong.song@autonavi.com</mailto>
 */
public class OrderServiceImpl implements IOrderService{
    @Override
    public boolean add(Order order) throws Exception {
        return order.save();
    }

    @Override
    public boolean delete(long id) throws Exception {
        return Order.dao.deleteById(id);
    }

    @Override
    public boolean update(Order order) throws Exception {
        return order.update();
    }

    @Override
    public void cancel(long orderId) throws Exception {
        String sql = "UPDATE `order` o SET o.`status` = 3 WHERE id = " + orderId;
        Db.update(sql);
    }


    @Override
    public Order findById(long id) throws Exception {
        String sql = "SELECT ha.`address`, ha.name AS contact, ha.tel, ha.`remark`, i.`name` AS iName, t.name AS tName, " +
                "oi.`num`, o.`begin_date`, o.`begin_time`, o.`coupon_price`, o.`pay_way`, o.`pay_status`,o.`sum_price`," +
                "o.`coupon_price`,o.`cash_price`,o.status FROM `order` o, his_address ha, order_item oi, item i, technician t " +
                "WHERE o.id = ha.`order_id` AND o.id = oi.`order_id` " +
                "AND o.`tech_id` = t.id AND i.`id` = oi.`item_id` AND o.`id` = " + id;
        return Order.dao.findFirst(sql);
    }

    @Override
    public Page<Order> findByPage(String openid,int pn,int ps) throws Exception {
        String select = "SELECT o.id AS order_id,o.create_time as oct, o.*, t.*, oi.*, i.name as item_name ";
        String from = " FROM `order` o, technician t, order_item oi, item i WHERE o.tech_id = t.id AND o.id = o.id and o.id = oi.order_id AND oi.item_id = i.id ";
        if(StringUtils.isNotBlank(openid)){
            from += " and o.open_id = '" + openid + "'";
        }
        String orderBy = " order by o.create_time desc ";
        System.out.println(select + from);
        Object[] param = {};
        return Order.dao.paginate(pn, ps, select + from, orderBy, param);
    }

    @Override
    public List<Order> findWT(long techId, String date) {
        String sql = "SELECT wt.time,w.time as iw FROM work_time wt LEFT JOIN (" +
                "SELECT * FROM work_time t WHERE t.time NOT IN (" +
                "SELECT o.begin_time FROM `order` o WHERE o.tech_id = " + techId + " " +
                "AND o.begin_date = '" + date + "') AND t.time NOT IN (" +
                "SELECT tr.rest_time FROM tech_rest tr WHERE tr.tech_id = " + techId +
                " AND tr.rest_date = '" + date + "')) AS w ON wt.id = w.id";
        List<Order> list = Order.dao.find(sql);
        if(list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    @Override
    public List<WorkTime> findAllWT() {
        return WorkTime.dao.find("select * from work_time;");
    }

//    SELECT
//    ha.name,
//    ha.address,
//    ha.tel,
//    i.name i_name,
//    t.name t_name,
//    t.tel t_tel,
//    i.spend_time,
//    oi.num,
//    o.begin_date,
//    o.begin_time
//            FROM
//    `order` o,
//    order_item oi,
//    `item` i,
//    his_address ha,
//    technician t
//    WHERE
//    o.id = oi.order_id
//    AND ha.order_id = o.id
//    AND i.id = oi.item_id
//    AND o.tech_id = t.id
//    AND o.id = 82;
    @Override
    public void sendOrderSMS(long orderId) {
        String sql = "SELECT ha.name, ha.address, ha.tel, i.name i_name, t.name t_name, t.tel t_tel,t.phone, i.spend_time, " +
                "oi.num, o.begin_date, o.begin_time FROM `order` o, order_item oi, `item` i, his_address ha, " +
                "technician t WHERE o.id = oi.order_id AND ha.order_id = o.id AND i.id = oi.item_id " +
                "AND o.tech_id = t.id AND o.id = " + orderId;
        List<Order> list = Order.dao.find(sql);
        if(list != null && list.size() > 0) {
            Order order = list.get(0);
            String uSMS = SMSUtil.makeOrderUserString(
                    order.getStr("begin_date"),
                    order.getStr("begin_time"),
                    order.getStr("i_name"),
                    order.getInt("spend_time")*order.getInt("num") + "",
                    order.getStr("t_name"),
                    order.getLong("t_tel")+"",
                    order.getStr("phone"));
            String tSMS = SMSUtil.makeOrderTechString(
                    order.getStr("begin_date"),
                    order.getStr("begin_time"),
                    order.getStr("i_name"),
                    order.getStr("name"),
                    order.getStr("address"),
                    order.getLong("tel")+"");
//            System.out.println(uSMS);
//            System.out.println(tSMS);
            SMSUtil.sendSMS(order.getLong("t_tel"), tSMS);
            SMSUtil.sendSMS(order.getLong("tel"), uSMS);
        }
    }

    public void sendOrderCancelSMS(long orderId) {
        String sql = "SELECT ha.name, ha.address, ha.tel, i.name i_name, t.name t_name, t.tel t_tel,t.phone, i.spend_time, " +
                "oi.num, o.begin_date, o.begin_time FROM `order` o, order_item oi, `item` i, his_address ha, " +
                "technician t WHERE o.id = oi.order_id AND ha.order_id = o.id AND i.id = oi.item_id " +
                "AND o.tech_id = t.id AND o.id = " + orderId;
        List<Order> list = Order.dao.find(sql);
        if(list != null && list.size() > 0) {
            Order order = list.get(0);
            String uSMS = SMSUtil.makeOrderCancleUserString(orderId);
            String tSMS = SMSUtil.makeOrderCancleTechString(
                    orderId,
                    order.getStr("begin_date"),
                    order.getStr("begin_time"),
                    order.getStr("i_name"),
                    order.getInt("spend_time") * order.getInt("num") + "",
                    order.getStr("t_name"),
                    order.getLong("t_tel") + "");
            SMSUtil.sendSMS(order.getLong("t_tel"), tSMS);
            SMSUtil.sendSMS(order.getLong("tel"), uSMS);
        }
    }
}
